package cn.xie.infrastructure.persistent.repository;

import cn.xie.domain.strategy.model.entity.StrategyAwardEntity;
import cn.xie.domain.strategy.model.entity.StrategyEntity;
import cn.xie.domain.strategy.model.entity.StrategyRuleEntity;
import cn.xie.domain.strategy.repository.IStrategyRepository;
import cn.xie.infrastructure.persistent.dao.IStrategyAwardDAO;
import cn.xie.infrastructure.persistent.dao.IStrategyDAO;
import cn.xie.infrastructure.persistent.dao.IStrategyRuleDAO;
import cn.xie.infrastructure.persistent.po.StrategyAwardPO;
import cn.xie.infrastructure.persistent.po.StrategyPO;
import cn.xie.infrastructure.persistent.po.StrategyRulePO;
import cn.xie.infrastructure.redis.IRedisService;
import cn.xie.types.common.Constants;
import org.springframework.stereotype.Repository;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: xiaoxie
 * create: 2025-08-03 17:57
 * @BelongsProject: bit-market
 * @BelongsPackage: cn.xie.infrastructure.persistent.repository
 * description: 策略奖品仓储
 */
@Repository
public class StrategyRepository implements IStrategyRepository {
    @Resource
    private IStrategyAwardDAO strategyAwardDAO;

    @Resource
    private IRedisService redisService;

    @Resource
    private IStrategyDAO strategyDAO;

    @Resource
    private IStrategyRuleDAO strategyRuleDAO;



    @Override
    public List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId) {
        // 先从缓存中获取
        String cacheKey = Constants.RedisKey.STRATEGY_AWARD_KEY + strategyId;
        List<StrategyAwardEntity> strategyAwardEntities = redisService.getValue(cacheKey);
        if(strategyAwardEntities != null && !strategyAwardEntities.isEmpty()) {
            return strategyAwardEntities;
        }
        List<StrategyAwardPO> strategyAwardPOLists = strategyAwardDAO.queryStrategyAwardListByStrategyId(strategyId);
        strategyAwardEntities = new ArrayList<>(strategyAwardPOLists.size());
        for(StrategyAwardPO strategyAwardPO : strategyAwardPOLists) {
            StrategyAwardEntity strategyAwardEntityList = StrategyAwardEntity.builder()
                    .strategyId(strategyAwardPO.getStrategyId())
                    .awardId(strategyAwardPO.getAwardId())
                    .awardCount(strategyAwardPO.getAwardCount())
                    .awardCountSurplus(strategyAwardPO.getAwardCount())
                    .awardRate(strategyAwardPO.getAwardRate())
                    .build();
                strategyAwardEntities.add(strategyAwardEntityList);
        }
        //存储到缓存
        redisService.setValue(cacheKey, strategyAwardEntities);
        return strategyAwardEntities;
    }
    /**
     * @description: 把策略奖品搜索的表存储到redis中
     * @author: xiaoxie
     * @date: 2025/8/4 下午7:39
     * @param: [strategyId, rateRange, strategyAwardSearchRateTable]
     * @return: void
     **/
    @Override
    public void storeStrategyAwardSearchRateTable(String key, Integer rateRange, Map<Integer, Integer> strategyAwardSearchRateTable) {
        redisService.setValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + key, rateRange);
        redisService.getMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + key)
                .putAll(strategyAwardSearchRateTable);
    }

    @Override
    public Integer getStrategyAwardAssemble(String key, Integer rateKey) {
       return redisService.getFromMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + key, rateKey);
    }

    @Override
    public int getRateRange(String key) {
        return redisService.getValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + key);
    }

    @Override
    public StrategyEntity queryStrategyByStrategyId(Long strategyId) {
        // 先从缓存中获取
        StrategyEntity serviceCatchValue = redisService.getValue(Constants.RedisKey.STRATEGY_KEY + strategyId);
        if(null != serviceCatchValue) {
            return serviceCatchValue;
        }
        StrategyPO strategyPO = strategyDAO.selectStrategyByStrategyId(strategyId);
        StrategyEntity strategy = StrategyEntity.builder()
                .strategyId(strategyId)
                .strategyDesc(strategyPO.getStrategyDesc())
                .ruleModels(strategyPO.getRuleModels())
                .build();
        redisService.setValue(Constants.RedisKey.STRATEGY_KEY + strategyId, strategy);
        return strategy;
    }

    @Override
    public StrategyRuleEntity queryStrategyRuleByStrategyId(Long strategyId,String ruleModel) {
      // 获取缓存
        StrategyRuleEntity strategyRuleCatch = redisService.getValue(Constants.RedisKey.STRATEGY_RULE_KEY + strategyId
                                                                          +Constants.UNDERSCORE + ruleModel);
        if(null != strategyRuleCatch) {
            return strategyRuleCatch;
        }
        StrategyRulePO strategyRule = strategyRuleDAO.queryStrategyRule(strategyId,ruleModel);
        StrategyRuleEntity strategyRuleEntity = StrategyRuleEntity.builder()
                .strategyId(strategyRule.getStrategyId())
                .awardId(strategyRule.getAwardId())
                .ruleType(strategyRule.getRuleType())
                .ruleModel(strategyRule.getRuleModel())
                .ruleValue(strategyRule.getRuleValue())
                .ruleDesc(strategyRule.getRuleDesc())
                .build();
        redisService.setValue(Constants.RedisKey.STRATEGY_RULE_KEY + strategyId
                +Constants.UNDERSCORE + ruleModel,strategyRuleEntity);
        return strategyRuleEntity;
    }

    @Override
    public String queryStrategyRuleValue(Long strategyId, Integer awardId, String ruleModel) {
        StrategyRulePO strategyRule = new StrategyRulePO();
        strategyRule.setStrategyId(strategyId);
        strategyRule.setAwardId(awardId);
        strategyRule.setRuleModel(ruleModel);
        return strategyRuleDAO.queryStrategyRuleValue(strategyRule);
    }

    @Override
    public StrategyRuleEntity queryStrategyRule(Long strategyId, Integer awardId, String ruleModel) {
        StrategyRulePO strategyRulePO = strategyRuleDAO.queryStrategyRuleEntity(strategyId, ruleModel, awardId);
        return StrategyRuleEntity.builder()
                .strategyId(strategyId)
                .awardId(awardId)
                .ruleType(strategyRulePO.getRuleType())
                .ruleModel(strategyRulePO.getRuleModel())
                .ruleValue(strategyRulePO.getRuleValue())
                .ruleDesc(strategyRulePO.getRuleDesc())
                .build();
    }


}
